const fs = require("fs");
const router = require('koa-router')()
const multer  = require('@koa/multer')
const upload = multer({ dest: './public/upload' });
const controller = require('../controller');
// x-response-time

router.get('/', async (ctx, next) => {
  ctx.body = 'Hello !'
})
router.get('/testapi', async (ctx, next) => {
  ctx.status = 401
  // ctx.body = {
  //   msg:'密码错误'
  // }
})
router.get('/testapi1', async (ctx, next) => {
  ctx.status = 401
  ctx.body = {
    msg:'用户名错误'
  }
})

router.post('/api/upload-single', upload.single('image'),async (ctx, next) => {
    let file = Date.parse(new Date())+Math.random().toString(36).substr(2,5).toUpperCase()+'.'+ctx.file.originalname.split('.').reverse()[0]
    fs.renameSync(ctx.file.path,ctx.file.destination+'/'+file);
    ctx.body = {
        code: 200,
        msg: '上传成功！',
        file: file
    }
})

router.get('/api/count',controller.count)
router.get('/api/tags/get',controller.tag.getTag) // 获取 话题标签
router.post('/api/tags/add',controller.tag.saveTag) // 添加 话题标签
router.post('/api/url/parse',controller.url.urlParse) // 获取网页信息
router.get('/api/code/checkcode', controller.code.getCode)// 验证码获取
router.post('/api/user/register', controller.user.register)// 注册
router.post('/api/user/login', controller.user.login)// 登陆
router.post('/api/user/update', controller.user.updateUser)// 更新用户信息
router.post('/api/user/info', controller.user.userinfo)// 获取用户信息
router.post('/api/artical/add', controller.commit.add)// 发文
router.get('/api/artical/get', controller.commit.get)// 获取文章列表
router.post('/api/zan/add', controller.zan.addZan) //新增点赞
router.post('/api/comment/add', controller.comment.add) //新增评论
router.get('/api/comment/get', controller.comment.get) //获取评论
router.get('/api/phone/add', controller.phone.deviceInfo) //统计机型
router.get('/api/phone/get', controller.phone.deviceList) //查看机型
module.exports = router